#!/bin/bash/python
# _*_ coding: utf-8 _*_
# file： web_attack_fdb
# date-time: 2023/9/8-23:02
# auth:hard work
# description

import requests
from tools_dev_and_sys.sys_secure.check_host_status import check_host_stat1
from tools_dev_and_sys.sys_secure.port_scan_util import portscanner
import ntplib
class Web_execute_base:
    client = ntplib.NTPClient()
    def __init__(self,ntp_ip,):
        self.ntp_ip = ntp_ip
        self.ntp_server = '10.11.0.85'
        self.time1,self.time2 = self.get_ntp_time()

    def config_web_request(self,url, data):
        try:
            res = requests.post(url=url.format(self.time1), data=data)
            print(res)
            if  200 == res.status_code:
                print("request ok" % url)
            else:
                print('error1',res)
        except Exception as e:
            print('error2', e)


    def get_ntp_time(self):
        # is_online = check_host_stat1(self.ntp_ip)
        # if is_online:
        #     is_open = portscanner(self.ntp_ip, 113)
        #     if is_open is not True:
        #         raise
        #     else:
        #         pass
        try:
            ret = self.client.request(self.ntp_ip, version=3, timeout=10)
            if ret:
                time1 = str(ret.tx_time).split('.')[0]
                time2 = str(ret.tx_time).split('.')[1]
                time_tmp = time1 + time2[:3]
                time_tmp1 = time_tmp.replace(time_tmp[4:5], "8", 1)
                return time_tmp, time_tmp1
            else:
                return False
        except TimeoutError as e:
            print(e)


if __name__ == '__main__':
    # dut_lst = ['10.11.0.208', '10.11.0.223', '10.11.0.218', '10.11.0.219', '10.11.0.192', '10.11.0.236', '10.11.0.200',
    #            '10.11.0.213',  '10.11.0.235', '10.11.0.216']
    _mac = '0000.1113.2532'
    dut_ip = '10.11.0.217'
    url = 'http://10.11.0.217/goform/web_mc_binding_cfg?{}'
    data = {'isdel':'false',
            'isadd':'true',
            'delIndex':'1',
            'mac_binding_port_id':'67178497',
            'mac_binding_mac':_mac,
            'mac_binding_action_flag':'1'}
    ntp_server = '10.11.0.85'
    # for dut_ip in dut_lst:

    web = Web_execute_base(dut_ip)
    web.config_web_request(url, data)
    # if is_online:
    #     is_open = portscanner(dut_ip, 113)
    #     if is_open is True:
            # web = Web_execute_base(dut_ip)

    # else:
    #     print("dut  not online %s" % dut_ip)

